<template>
  <div class="statistics-container">
    <!-- 统计卡片 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>问卷总数</span>
              <el-icon><Document /></el-icon>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ statistics.totalQuestionnaires }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>用户总数</span>
              <el-icon><User /></el-icon>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ statistics.totalUsers }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>今日提交</span>
              <el-icon><Upload /></el-icon>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ statistics.todaySubmissions }}</h2>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>总提交数</span>
              <el-icon><View /></el-icon>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ statistics.totalSubmissions }}</h2>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 趋势图 -->
    <el-card class="mt-20">
      <template #header>
        <div class="card-header">
          <span>提交趋势</span>
        </div>
      </template>
      <div ref="chartRef" style="height: 400px"></div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick, onUnmounted } from 'vue'
import { Document, User, Upload, View } from '@element-plus/icons-vue'
import * as echarts from 'echarts'
import request from '@/utils/request'

const statistics = ref({
  totalQuestionnaires: 0,
  totalUsers: 0,
  todaySubmissions: 0,
  totalSubmissions: 0,
  dailyStats: []
})

const chartRef = ref(null)
let chart = null

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: statistics.value.dailyStats.map(item => item.date)
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '提交数',
        type: 'line',
        data: statistics.value.dailyStats.map(item => item.submissions),
        smooth: true
      }
    ]
  }
  chart.setOption(option)
}

const loadData = async () => {
  try {
    const res = await request.get('/api/questionnaire/statistics')
    if (res.code === 200) {
      statistics.value = res.data
      nextTick(() => {
        initChart()
      })
    }
  } catch (error) {
    console.error('加载数据失败:', error)
  }
}

const resizeChart = () => {
  chart?.resize()
}

onMounted(() => {
  loadData()
  window.addEventListener('resize', resizeChart)
})

onUnmounted(() => {
  window.removeEventListener('resize', resizeChart)
  chart?.dispose()
})
</script>

<style scoped>
.statistics-container {
  padding: 20px;
  height: calc(100vh - 60px); /* 减去顶部导航栏高度 */
  overflow-y: auto;
}
.mt-20 {
  margin-top: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-content {
  text-align: center;
}
</style> 